<?php 
	namespace Models;

	class Series
	{	
		private $app;
		private $access = 2;
		function __construct($app){
			$this->app = $app;
		}

		function getAll(){
			$sql = "SELECT idseries series_id, series_type, series_name, series_season, series_count, series_link, series_date FROM series";
			return json_encode($this->app['db']->fetchAll($sql));
		}

		function getOne($id){
			$sql = "SELECT idseries series_id, series_type, series_name, series_season, series_count, series_link, series_date FROM series WHERE idseries = ? ";
			return $this->app['db']->fetchAssoc($sql, array( (int) $id ));	
		}

		function insert($data){
			echo "insert";
			$this->app['db']->insert('series', $data);
			$id = $this->app['db']->lastInsertId();
			return array('series_id'=>$id);
		}

		function update($id, $data){
			$data_arr = array(
				$data['series_type'],
				$data['series_name'],
				$data['series_season'],
				$data['series_count'],
				$data['series_link'],
				$id
			);
			$sql = "UPDATE series SET series_type = ?, series_name = ?, series_season = ?, series_count = ?, series_link = ?,  series_date = now() WHERE idseries = ?";
			$this->app['db']->executeQuery($sql, $data_arr );
			return $res;
		}

		function inc($id){
			$sql_fetch = "SELECT series_count FROM series WHERE idseries = ?";
			$res =  $this->app['db']->fetchAssoc( $sql_fetch, array( (int) $id ) );
			$count = $res['series_count']+1;
			$sql = "UPDATE series SET series_count = ? , series_date = now() WHERE idseries = ?";
			$this->app['db']->executeQuery($sql, array(  (int) $count,  (int) $id ));
			$sql_fetch = "SELECT series_count FROM series WHERE idseries = ?";
			$res =  $this->app['db']->fetchAssoc( $sql_fetch, array( (int) $id ) );
			return $res;
		}		

		function delete($id){
			$this->app['db']->delete('series', array('idseries'=>$id ));
		}

	}

?>